// pages/manage/manage.js
const app = getApp();
Page({

    data: {
        listitem: {
            ProjectName: "",
            RevealImg: "",
            Summary: "",
            Price: "",
            Status: ""
        },
        iftype: "1",
        id: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      app.globalData.tempUrl="";
        var title = options.iftype == 1 ? "项目新增" : "项目编辑";
        wx.setNavigationBarTitle({
            title: title,
        })
        this.setData({
            "iftype": options.iftype,
            "id": options.id
        })

        var iftype = options.iftype;
        var id = options.id;
        //判断状态是修改还是添加，1为添加
        if (iftype == 1) {
            console.log("添加")
        } else {
            wx.showLoading({
                title: '加载中',
            })
            app.http.POST("/MiniCardProject/GetMyProject", { "id": id }, (err, data) => {
                if (data.code == 1) {
                    this.setData({
                        "listitem": data.data
                    })
                    wx.hideLoading()
                    console.log(data);
                } else {
                    wx.showToast({
                        title: '请求失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
            console.log("修改要初始化")
        }

    },
    //点击提交
    formSubmit: function (e) {
        var _this = this;
        var key = e.detail.value;
        var RevealImg = _this.data.listitem.RevealImg;
        if (key.ProjectName == "") {
            wx.showToast({
                title: '请输入项目名称',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (RevealImg == "") {
            wx.showToast({
                title: '请上传项目展示图',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (key.Summary == "") {
            wx.showToast({
                title: '请输入项目介绍',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (key.Price == "") {
            wx.showToast({
                title: '请输入价格',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        var Status;
        if (key.Status) {
            Status = 1
        } else {
            Status = 0
        }

        var iftype = _this.data.iftype;
        var id = _this.data.id;
        var dataArr = {};
        dataArr.ProjectName = key.ProjectName;
        dataArr.RevealImg = RevealImg;
        dataArr.Summary = key.Summary;
        dataArr.Price = key.Price;
        dataArr.Status = Status;
        console.log(dataArr);
        if (iftype == 1) {
            app.http.POST("/MiniCardProject/InsertProject", dataArr, (err, data) => {
                if (data.code == 1) {
                    wx.showToast({
                        title: '添加成功',
                        icon: '',
                        duration: 2000,
                        success: function (e) {
                            var pages = getCurrentPages();
                            if (pages[pages.length - 2].pName == 'manglist') {
                                pages[pages.length - 2].isUpdate = true;
                            }
                            wx.navigateBack();
                        }
                    })
                } else {
                    wx.showToast({
                        title: '请求失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        } else {
            dataArr.Id = id;
            app.http.POST("/MiniCardProject/UpdateProject", dataArr, (err, data) => {
                if (data.code == 1) {
                    wx.showToast({
                        title: '修改成功',
                        icon: '',
                        duration: 2000,
                        success: function (e) {
                            var pages = getCurrentPages();
                            if (pages[pages.length - 2].pName == 'manglist') {
                                pages[pages.length - 2].isUpdate = true;
                            }
                            wx.navigateBack();
                        }
                    })
                } else {
                    wx.showToast({
                        title: '修改失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            })

        }
    },
    //点击上传图片
    upimgbut: function () {
        wx.navigateTo({
            url: '../imgupload/imgupload?photoid=2'
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      console.log("tempUrl", app.globalData.tempUrl)
        if (this.data.isOpen) {
            return;
        }
        if (app.globalData.tempUrl) {
            this.setData({
              "listitem.RevealImg": app.globalData.tempUrl
            });
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.data.isOpen = false;
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var userInfo = app.globalData.userInfo;
        //判断当前用户是否开通服务,开通则分享美名片页,否则分享访问记录页
        if (userInfo == null || typeof (userInfo) == "undefined") {
            app.getUserall(function (res) {
                userInfo = res;
            });
        }
        var path = userInfo.UserType == 2 ? "/pages/index/index?cid=" + userInfo.Id : "/pages/cardrecord/cardrecord";
        return {
            title: userInfo.NickName + "的美名片",
            path: path,
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                    icon: "success",
                    duration: 200
                })
            }
        }
    }
})